﻿using SF.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SF.Ucenter.admin.customer
{
    public partial class cur_top : Web.UI.ManagePage
    {
        protected int totalCount;
        protected int page;
        protected int pageSize;
        protected string keywords = string.Empty, isshow = "hide";
        //&&1 待支付 0  支付完成
        protected int ddstatus;
        //&&1 在线支付 0 货到付款
        protected int ddtime;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.keywords = DTRequest.GetQueryString("keywords");
            this.ddstatus = DTRequest.GetQueryInt("ddlStatus");
            this.ddtime = DTRequest.GetQueryInt("ddlTime");
            this.isshow = string.IsNullOrEmpty(DTRequest.GetQueryString("isshow")) ? isshow : DTRequest.GetQueryString("isshow");
            this.pageSize = GetPageSize(9); //每页数量
            if (!Page.IsPostBack)
            {
                //ChkAdminLevel("sys_customer_top", DTEnums.ActionEnum.View.ToString()); //检查权限

                RptBind(ddstatus, ddtime, isshow);
                ClientScript.RegisterStartupScript(this.GetType(), "hideddTime", "<script>$('#ddTime')." + this.isshow + "()</script>");
            }
        }

        #region 数据绑定
        private void RptBind( int ddstatus, int ddtime, string ishow)
        {
            this.page = DTRequest.GetQueryInt("page", 1);
            if (ddstatus >= 0)
            {
                this.ddlStatus.SelectedIndex = ddstatus;
            }
            if (ddtime >= 0)
            {
                this.ddlTime.SelectedIndex = ddtime;
            }
            txtKeywords.Text = this.keywords;
            //商户 公众号 模版等信息
            Model.manager mgr = Session[DTKeys.SESSION_ADMIN_INFO] as Model.manager;
            string busnieseNum = mgr.busnieseNum.ToString();
            string appNum = Session[DTKeys.SESSION_APP_INFO].ToString();
            string templatesNum = Session[DTKeys.SESSION_TEMPLATES_INFO].ToString();

            BLL.customer bll = new BLL.customer(busnieseNum, appNum, templatesNum);
            this.rptList.DataSource = bll.GetList(this.pageSize, this.page, CombSqlTxt(this.keywords), out this.totalCount, this.ddlStatus.SelectedIndex, this.ddlTime.SelectedIndex);
            this.rptList.DataBind();


            //绑定页码
            txtPageNum.Text = this.pageSize.ToString();
            string pageUrl = Utils.CombUrlTxt("cur_top.aspx", "ddlStatus={0}&ddlTime={1}&keywords={2}&isshow={3}&page={4}", this.ddlStatus.SelectedIndex.ToString(), this.ddlTime.SelectedIndex.ToString(), this.keywords, ishow, "__id__");
            PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, pageUrl, 8);
        }
        #endregion

        //榜单类型 下拉框
        protected void ddlStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.ddlStatus.SelectedIndex == 0)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "hideddTime", "<script>$('#ddTime').hide()</script>");
                Response.Redirect(Utils.CombUrlTxt("cur_top.aspx", "ddlStatus={0}&ddlTime={1}&keywords={2}&isshow={3}", this.ddlStatus.SelectedIndex.ToString(), this.ddlTime.SelectedIndex.ToString(), this.keywords, "hide"));
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "showddTime", "<script>$('#ddTime').show()</script>");
                Response.Redirect(Utils.CombUrlTxt("cur_top.aspx", "ddlStatus={0}&ddlTime={1}&keywords={2}&isshow={3}", this.ddlStatus.SelectedIndex.ToString(), this.ddlTime.SelectedIndex.ToString(), this.keywords, "show"));
            }
        }

        //榜单时间类型 下拉框
        protected void ddlTime_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("cur_top.aspx", "ddlStatus={0}&ddlTime={1}&keywords={2}&isshow={3}", this.ddlStatus.SelectedIndex.ToString(), this.ddlTime.SelectedIndex.ToString(), this.keywords, isshow));
        }

        #region 组合SQL查询语句
        protected string CombSqlTxt(string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();
            _keywords = _keywords.Replace("'", "");
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append(" userNum like  '%")
                      .Append(_keywords)
                      .Append("%' or nickName like '%")
                      .Append(_keywords)
                      .Append("%'");
            }
            return strTemp.ToString();
        }
        #endregion

        #region 返回每页数量
        private int GetPageSize(int _default_size)
        {
            int _pagesize;
            if (int.TryParse(Utils.GetCookie("cur_top_page_size"), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    return _pagesize;
                }
            }
            return _default_size;
        }
        #endregion

        //关健字查询
        protected void lbtnSearch_Click(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("cur_top.aspx", "ddlStatus={0}&ddlTime={1}&keywords={2}&isshow={3}", this.ddlStatus.SelectedIndex.ToString(), this.ddlTime.SelectedIndex.ToString(), this.txtKeywords.Text, isshow));
        }

        //设置分页数量
        protected void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            int _pagesize;
            if (int.TryParse(txtPageNum.Text.Trim(), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    Utils.WriteCookie("cur_top_page_size", _pagesize.ToString(), 14400);
                }
            }
            Response.Redirect(Utils.CombUrlTxt("cur_top.aspx", "ddlStatus={0}&ddlTime={1}&keywords={2}&isshow={3}", this.ddlStatus.SelectedIndex.ToString(), this.ddlTime.SelectedIndex.ToString(), this.keywords, isshow));
        }
    }
}